<?php

namespace app\api\controller;

use app\api\common\controller\Api;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("Demo.php")
 * @Apidoc\Group("api")
 */
class Demo extends Api
{
    /**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = ['*'];

    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = [];

    // {__CONTENT__}

    /** function demoList1()
     * @Apidoc\Title("列表")
     * @Apidoc\Desc("列表接口")
     * @Apidoc\Url("/api/Demo/demoList1")
     * @Apidoc\Method("POST")
     * @Apidoc\Tag("tag")
     * @Apidoc\Header(ref="auth")
     * @Apidoc\Param(ref="pagingParam")
     * @Apidoc\Param("name",type="string",require="1",desc="1")
     * @Apidoc\Returned(ref="pagingParam")
     *
     * @Apidoc\Returned("data",type="array",ref="\app\api\model\User\getFieldsDesc")
     */
    public function demoList1()
    {
        $result = $this->validate(
            [
                'name' => input('name'),

            ],
            [
                'name' => 'require',

            ],
            [
                'name' => '名称不能为空',

            ]
        );
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }

        $result =
            [

            ];

        try {
            $res = \app\api\model\User::where(['name' => input('name')])->paginate();
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('ok', $res);
    } // function demoList1()


    /** function add()
     * @Apidoc\Title("添加")
     * @Apidoc\Desc("添加接口")
     * @Apidoc\Url("/api/Demo/add")
     * @Apidoc\Method("POST")
     * @Apidoc\Tag("tag")
     * @Apidoc\Header(ref="auth")
     *
     * @Apidoc\Param("name",type="string",require="1",desc="1")
     *
     *
     *

     */
    public function add()
    {
        $result = $this->validate(
            [
                'name' => input('name'),

            ],
            [
                'name' => 'require',

            ],
            [
                'name' => '名称不能为空',

            ]
        );
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }

        $result =
            [
                'name' => input('name'),

            ];

        try {
            $res = \app\api\model\User::insert($result);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('ok', $res);
    } // function add()

    /** function edit()
     * @Apidoc\Title("添加")
     * @Apidoc\Desc("添加接口")
     * @Apidoc\Url("/api/Demo/edit")
     * @Apidoc\Method("POST")
     * @Apidoc\Tag("tag")
     * @Apidoc\Header(ref="auth")
     *
     * @Apidoc\Param("name",type="string",require="1",desc="1")
     *
     *
     * @Apidoc\Returned("data",type="array",ref="\app\api\model\User\getFieldsDesc")
     */
    public function edit()
    {
        $result = $this->validate(
            [
                'name' => input('name'),

            ],
            [
                'name' => 'require',

            ],
            [
                'name' => '名称不能为空',

            ]
        );
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }

        $result =
            [
                'name' => input('name'),

            ];

        try {
            $res = \app\api\model\User::where(['name' => input('name'),])->find();
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('ok', $res);
    } // function edit()

}